package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.goods.entity.category.BrandCategory;
import com.hoshiicloud.goods.entity.goods.Gallery;
import com.hoshiicloud.goods.entity.sku.BaseSku;
import com.hoshiicloud.goods.entity.sku.BaseSkuGallery;
import com.hoshiicloud.goods.entity.spu.ExtSpuTags;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.goods.GalleryMapper;
import com.hoshiicloud.goods.mapper.sku.BaseSkuGalleryMapper;
import com.hoshiicloud.goods.service.BaseSkuGalleryService;
import com.hoshiicloud.goods.service.GalleryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 基础SKU商品图片资源 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class GalleryServiceImpl extends ServiceImpl<GalleryMapper, Gallery> implements GalleryService {

    @Autowired
    private IdGeneratorService idGeneratorService;


    /**
     * 保存相册信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "新增相册表信息")
    public Gallery saveGallery(Gallery entity) {

        // 生成主键ID
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreateTime(LocalDateTime.now());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return entity;
    }

    /**
     * 根据Id 更新相册信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "相册表修改失败")
    public boolean updateGalleryById(Gallery entity) {
        // 更新数据
        entity.setUpdateTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "商品管理-商品相册删除")
    public boolean deleteGalleryById(Long id) {

        // 根据id查询数据
        Gallery entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("相册删除失败，无法找到相关信息");
        }

        //进行删除操作
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }


    /**
     * 根据id集合进行批量删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "根据ID集合进行批量删除")
    public Boolean deleteBatchs(List<Long> ids) {

        QueryWrapper<Gallery> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Gallery::getId, ids).eq(Gallery::getDeleted, 0);

        Gallery update = new Gallery();
        update.setDeleted(1);

        baseMapper.update(update, wrapper);
        return true;
    }

    /**
     *  根据自定义条件查询相册信息
     */
    //@Cacheable(cacheNames = "Gallery",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public List<Gallery> getGalleryByWrapper(QueryWrapper wrapper, String key) {
        List list = baseMapper.selectList(wrapper);
        if (list.isEmpty()) {
            throw new ServiceException(CommonError.error("信息查询失败"));
        }
        return list;
    }

    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "根据自定义条件批量更新")
    @Override
    public boolean updateBatchByWrapper(UpdateBatchParam<Gallery> updateBatchParam) {
        Gallery updateEntity = updateBatchParam.getUpdateEntity();
        QueryWrapper<Gallery> wrapper = updateBatchParam.getWrapper();
        return this.update(updateEntity, wrapper);
    }

    /**
     * 批量新增相册信息
     */
    @Override
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "批量新增相册信息")
    public boolean saveGalleryBatch(List<Gallery> galleries) {
        boolean isTrue = this.saveBatch(galleries);
        if (!isTrue) {
            throw new ServiceException("批量新增相册信息失败！");
        }
        return true;
    }

    /**
     * 自定义批量查询相册信息
     */
    @Override

    public List<Gallery> queryBatch(List<OmnipotentWrapper> param) {
        OmnipotentWrapper<Gallery> wrapper = new OmnipotentWrapper<>();
        QueryWrapper<Gallery> queryWrapper = wrapper.getWrapper(param);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "根据Id删除相册信息")
    public boolean deleteById(Long id) {

        // 根据id查询相册信息是否存在
        Gallery entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("删除相册信息异常， 无法找到相关记录");
        }

        // 删除商品
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    /**
     * 批量更新
     */
    @Override
    @CacheEvict(cacheNames = "Gallery", allEntries = true)
    @Logable(authName = "批量更新相册信息")
    public Boolean updateBatch(OmnipotentParam<Gallery> param) {
        OmnipotentWrapper<Gallery> wrapper = new OmnipotentWrapper<>();
        UpdateWrapper<Gallery> updateWrapper = wrapper.getUpdateWrapper(param.getWrappers());
        Gallery entity = param.getEntity();
        return this.update(entity, updateWrapper);
    }
}
